package com.ruoyi.web.controller.admin;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.BdmMemberLevel;
import com.ruoyi.system.service.IBdmMemberLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员等级Controller
 * 
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/admin/level")
public class BdmMemberLevelController extends BaseController
{
    @Autowired
    private IBdmMemberLevelService bdmMemberLevelService;

    /**
     * 查询会员等级列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BdmMemberLevel bdmMemberLevel)
    {
        startPage();
        List<BdmMemberLevel> list = bdmMemberLevelService.selectBdmMemberLevelList(bdmMemberLevel);
        return getDataTable(list);
    }

    /**
     * 导出会员等级列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdmMemberLevel bdmMemberLevel)
    {
        List<BdmMemberLevel> list = bdmMemberLevelService.selectBdmMemberLevelList(bdmMemberLevel);
        ExcelUtil<BdmMemberLevel> util = new ExcelUtil<BdmMemberLevel>(BdmMemberLevel.class);
        util.exportExcel(response, list, "会员等级数据");
    }

    /**
     * 获取会员等级详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bdmMemberLevelService.selectBdmMemberLevelById(id));
    }

    /**
     * 新增会员等级
     */
    @PostMapping
    public AjaxResult add(@RequestBody BdmMemberLevel bdmMemberLevel)
    {
        BdmMemberLevel select = new BdmMemberLevel();
        select.setMemberLevelType(bdmMemberLevel.getMemberLevelType());
        List<BdmMemberLevel> levelList = bdmMemberLevelService.selectBdmMemberLevelList(select);
        if (!CollectionUtils.isEmpty(levelList)) {
            throw new RuntimeException("该等级已存在");
        }
        return toAjax(bdmMemberLevelService.insertBdmMemberLevel(bdmMemberLevel));
    }

    /**
     * 修改会员等级
     */
    @PutMapping
    public AjaxResult edit(@RequestBody BdmMemberLevel bdmMemberLevel)
    {
        BdmMemberLevel select = new BdmMemberLevel();
        select.setMemberLevelType(bdmMemberLevel.getMemberLevelType());
        List<BdmMemberLevel> levelList = bdmMemberLevelService.selectBdmMemberLevelList(select);
        if (!CollectionUtils.isEmpty(levelList) && levelList.size() > 1) {
            throw new RuntimeException("该等级已存在");
        }
        return toAjax(bdmMemberLevelService.updateBdmMemberLevel(bdmMemberLevel));
    }

    /**
     * 删除会员等级
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bdmMemberLevelService.deleteBdmMemberLevelByIds(ids));
    }
}
